import { DaliyTaskData, GroupTaskData } from "./GameDefine";
import { TaskMgr } from "./TaskMgr";
import { GameData } from "./GameData";
import { GameState } from "./GameState";
cc.Class({
    extends: require('BaseUI'),

    properties: {
        nodBtnNoComplete: cc.Node,
        nodBtnComplete: cc.Node,
        nodBtnAchieved: cc.Node,
        labDesc: cc.Label,
        labReward: cc.Label,
        labCount: cc.Label,
    },

    onLoad() {
        this._super();
    },

    onEnable() {
        if (!this._inited) {
            return;
        }
        this._updateLabel();
        this._updateBtn();
    },

    init(type, index) {
        this._index = index;
        this._type = type;
        this._inited = true;
        this._updateLabel();
        this._updateBtn();
    },

    _updateLabel() {
        let taskData;
        if (this._type == 1) {
            // 日常
            taskData = DaliyTaskData[this._index];
        } else {
            taskData = GroupTaskData[this._index];
        }

        this.labDesc.string = taskData.taskDesc;
        this.labReward.string = taskData.rewardDesc;

        let data;
        if (this._type == 1) {
            // 日常
            data = TaskMgr.getDaliyTask(this._index);
        } else {
            data = TaskMgr.getGroupTask(this._index);
        }

        let targetCount;
        let currCount;
        if (taskData.taskType == 3) {
            // 金币
            targetCount = G.bignumber.toUnitType(data[0]);
            currCount = G.bignumber.toUnitType(data[1]);
        } else {
            targetCount = data[0];
            currCount = data[1];
        }

        this.labCount.string = `(${currCount}/${targetCount})`;
    },

    _updateBtn() {
        // 按钮
        let data;
        if (this._type == 1) {
            // 日常
            data = TaskMgr.getDaliyTask(this._index);
        } else {
            data = TaskMgr.getGroupTask(this._index);
        }

        if (data[2] == 1) {
            this.nodBtnNoComplete.active = false;
            this.nodBtnComplete.active = false;
            this.nodBtnAchieved.active = true;
        } else {
            if (G.bignumber.compose(data[1], data[0]) >= 0) {
                this.nodBtnNoComplete.active = false;
                this.nodBtnComplete.active = true;
                this.nodBtnAchieved.active = false;
            } else {
                this.nodBtnNoComplete.active = true;
                this.nodBtnComplete.active = false;
                this.nodBtnAchieved.active = false;
            }
        }
    },

    buttonListener: function (button) {
        G.game.gameAudioMgr.playBtn();
        let name = button.node.name;
        if (name == 'btnComplete') {
            let taskData;
            if (this._type == 1) {
                // 日常
                taskData = DaliyTaskData[this._index];
                TaskMgr.setDaliyTaskAchieve(this._index);

            } else {
                TaskMgr.setGroupTaskAchieve(this._index);
                taskData = GroupTaskData[this._index];
            }

            if (taskData.rewardType == 1) {
                // 钻石
                GameData.addDiamond(taskData.rewardCount);
                G.game.toast('钻石+' + taskData.rewardCount, 1);
            } else if (taskData.rewardType == 2) {
                // 金币
                let second = taskData.taskCount * 60;
                let coin = GameState.getProduction(second);
                if (G.bignumber.compose(coin, 0) == 0) {
                    coin = 1000;
                }
                GameData.addCoin(coin);
                G.game.toast('金币+' + G.bignumber.toUnitType(coin), 1);
            }

            this._updateBtn();
        }
    },
});
